<template>
  <div>
    <el-row style="line-height: 60px">
      <el-col :span="5">
        <el-input v-model="gname" placeholder="商品名称"></el-input>
      </el-col>
      <el-col :span="5">
        <el-select v-model="typeId" placeholder="请选择商品类型">
          <el-option :value="0" label="请选择商品类型"></el-option>
          <el-option
              v-for="type in typeList"
              :key="type.typeid"
              :value="type.typeid"
              :label="type.typename">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="2">
        <el-button type="success" @click="queryGoods(1)">搜索</el-button>
      </el-col>
      <el-col :span="12">
        <el-button type="primary" @click="dialogAddVisible = true">添加</el-button>
      </el-col>
    </el-row>
    <el-table
        :data="goodsList"
        border
        style="width: 90%; line-height: 60px">
      <el-table-column
          label="商品名称"
          width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.gname }}</span>
        </template>
      </el-table-column>
      <el-table-column
          label="商品类型"
          width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.typename }}</span>
        </template>
      </el-table-column>
      <el-table-column
          label="商品数量">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.count }}</span>
        </template>
      </el-table-column>
        <el-table-column
            label="操作">
          <template slot-scope="scope">
            <el-button type="warning" size="mini" @click="showUpdateView(scope.row.gid)">修改</el-button>
            <el-button type="danger" size="mini" @click="deleteGoods(scope.row.gid)">删除</el-button>
           </template>
        </el-table-column>
    </el-table>
    <el-pagination
        background
        layout="prev, pager, next"
        :total="pageInfo.total"
        :page-size="pageInfo.pageSize"
        @prev-click="queryGoods"
        @next-click="queryGoods"
        @current-change="queryGoods">
    </el-pagination>


    <el-dialog title="修改商品" :visible.sync="dialogFormVisible">
      <el-form :model="updateGoods">
        <el-form-item label="商品名称" label-width="120px">
          <el-input v-model="updateGoods.gname"  autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品类型" label-width="120px">
          <el-select v-model="updateGoods.typeId" autocomplete="off">
            <el-option
                v-for="type in typeList"
                :key="type.typeid"
                :value="type.typeid"
                :label="type.typename">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品数量" label-width="120px">
          <el-input v-model="updateGoods.count" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="upGoods">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="添加商品" :visible.sync="dialogAddVisible">
      <el-form :model="addGoods">
        <el-form-item label="商品名称" label-width="120px">
          <el-input v-model="addGoods.gname"  autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品类型" label-width="120px">
          <el-select v-model="addGoods.typeId" autocomplete="off">
            <el-option
                v-for="type in typeList"
                :key="type.typeid"
                :value="type.typeid"
                :label="type.typename">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品数量" label-width="120px">
          <el-input v-model="addGoods.count" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogAddVisible = false">取 消</el-button>
        <el-button type="primary" @click="mAddGoods">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

export default {
  name: "GoodsList",
  data() {
    return {
      goodsList:[],
      gname: "",
      typeId: 0,
      pageInfo: {},
      typeList: [],
      dialogFormVisible: false,
      dialogAddVisible: false,
      updateGoods: {
        gid: 0,
        gname: '',
        typeId: 0,
        count: 0
      },
      addGoods: {
        gid: 0,
        gname: '',
        typeId: 1,
        count: 0
      }
    }
  },
  methods: {
    queryGoods(page) {
      this.axios.get("http://localhost:8080/goods/list", {
        params: {
          gname: this.gname,
          typeId: this.typeId,
          now: page
        }
      }).then(data => {
        this.goodsList = data.data.data.list;
        this.pageInfo = data.data.data;
        console.log(data)
      })
    },
    queryType() {
      this.axios.get("http://localhost:8080/goods/queryType")
          .then(data => {
            this.typeList = data.data.data;
            console.log(this.typeList)
          })
    },
    deleteGoods(gid) {
      this.axios.delete("http://localhost:8080/goods/deleteGoods/" + gid)
          .then(data => {
            if (data.data.code == 200) {
              this.$message({
                message: "删除成功",
                type: "success"
              })
              this.queryGoods(1);
            } else {
              this.$message({
                message: "删除失败",
                type: "danger"
              })
            }
          })
    },
    showUpdateView(gid) {
      this.dialogFormVisible = true;
      this.axios.get("http://localhost:8080/goods/queryGoodsById/" + gid)
          .then(data => {
              this.updateGoods = data.data.data;
          })
    },
    upGoods() {
      this.axios.put("http://localhost:8080/goods/updateGoods", this.qs.stringify(this.updateGoods))
          .then(data => {
            console.log(data);
            if (data.data.code == 200) {
              this.queryGoods(1);
              this.$message({
                message: "修改成功",
                type: "success"
              })
              this.dialogFormVisible = false;
            } else {
              this.$message({
                message: "修改失败",
                type: "danger"
              })
            }
      })
    },
    mAddGoods() {
      this.axios.post("http://localhost:8080/goods/saveGoods", this.qs.stringify(this.addGoods))
          .then(data => {
            if (data.data.code == 200) {
              this.queryGoods(1);
              this.$message({
                message: "添加成功",
                type: "success"
              })
              this.dialogAddVisible = false;
            } else {
              this.$message({
                message: "添加成功",
                type: "danger"
              })
            }
          })
    }
  },
  mounted() {
    this.queryGoods();
    this.queryType();
  }
}
</script>

<style scoped>
  .el-select {
    width: 100%;
  }
</style>